package com.xujie.sportsmeeting.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xujie.sportsmeeting.common.result.Result;
import com.xujie.sportsmeeting.dto.EventCheckinDTO;
import com.xujie.sportsmeeting.service.EventCheckinService;
import com.xujie.sportsmeeting.vo.EventCheckinVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 赛事检录控制器
 */
@Tag(name = "赛事检录管理", description = "赛事检录相关接口")
@RestController
@RequestMapping("/checkin")
@RequiredArgsConstructor
public class EventCheckinController {

    private final EventCheckinService eventCheckinService;

    @Operation(summary = "分页查询检录信息")
    @GetMapping("/page")
    public Result<IPage<EventCheckinVO>> getCheckinPage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "20") Integer size,
            @Parameter(description = "比赛项目ID") @RequestParam(required = false) Long eventId,
            @Parameter(description = "运动员姓名") @RequestParam(required = false) String athleteName,
            @Parameter(description = "学号") @RequestParam(required = false) String studentNumber,
            @Parameter(description = "院系") @RequestParam(required = false) String department,
            @Parameter(description = "检录状态") @RequestParam(required = false) Integer checkinStatus) {
        
        Page<EventCheckinVO> page = new Page<>(current, size);
        IPage<EventCheckinVO> result = eventCheckinService.getCheckinPage(
            page, eventId, athleteName, studentNumber, department, checkinStatus);
        
        return Result.success(result);
    }

    @Operation(summary = "根据比赛项目ID查询检录信息")
    @GetMapping("/event/{eventId}")
    public Result<List<EventCheckinVO>> getCheckinByEventId(@PathVariable Long eventId) {
        List<EventCheckinVO> result = eventCheckinService.getCheckinByEventId(eventId);
        return Result.success(result);
    }

    @Operation(summary = "创建检录记录")
    @PostMapping("/create/{eventId}")
    public Result<Void> createCheckinRecords(@PathVariable Long eventId) {
        boolean success = eventCheckinService.createCheckinRecords(eventId);
        return success ? Result.success("检录记录创建成功") : Result.error("检录记录创建失败");
    }

    @Operation(summary = "运动员检录")
    @PostMapping("/checkin")
    public Result<Void> checkinAthlete(@Valid @RequestBody EventCheckinDTO dto) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String operator = auth.getName();
        
        boolean success = eventCheckinService.checkinAthlete(dto, operator);
        return success ? Result.success("检录成功") : Result.error("检录失败");
    }

    @Operation(summary = "批量检录")
    @PostMapping("/batch-checkin")
    public Result<Void> batchCheckin(@RequestBody List<Long> checkinIds) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String operator = auth.getName();
        
        boolean success = eventCheckinService.batchCheckin(checkinIds, operator);
        return success ? Result.success("批量检录成功") : Result.error("批量检录失败");
    }

    @Operation(summary = "标记缺席")
    @PostMapping("/absent/{checkinId}")
    public Result<Void> markAbsent(
            @PathVariable Long checkinId, 
            @RequestParam(required = false) String notes) {
        
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String operator = auth.getName();
        
        boolean success = eventCheckinService.markAbsent(checkinId, operator, notes);
        return success ? Result.success("标记缺席成功") : Result.error("标记缺席失败");
    }

    @Operation(summary = "取消检录")
    @PostMapping("/cancel/{checkinId}")
    public Result<Void> cancelCheckin(@PathVariable Long checkinId) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String operator = auth.getName();
        
        boolean success = eventCheckinService.cancelCheckin(checkinId, operator);
        return success ? Result.success("取消检录成功") : Result.error("取消检录失败");
    }

    @Operation(summary = "获取检录统计信息")
    @GetMapping("/statistics/{eventId}")
    public Result<Map<String, Object>> getCheckinStatistics(@PathVariable Long eventId) {
        Map<String, Object> statistics = eventCheckinService.getCheckinStatistics(eventId);
        return Result.success(statistics);
    }

    @Operation(summary = "查询未检录的运动员列表")
    @GetMapping("/unchecked/{eventId}")
    public Result<List<EventCheckinVO>> getUncheckedAthletes(@PathVariable Long eventId) {
        List<EventCheckinVO> result = eventCheckinService.getUncheckedAthletes(eventId);
        return Result.success(result);
    }

    @Operation(summary = "验证运动员是否已检录")
    @GetMapping("/verify")
    public Result<Boolean> verifyCheckin(
            @RequestParam Long athleteId, 
            @RequestParam Long eventId) {
        
        boolean isCheckedIn = eventCheckinService.isAthleteCheckedIn(athleteId, eventId);
        return Result.success(isCheckedIn);
    }

    @Operation(summary = "根据运动员和比赛查询检录记录")
    @GetMapping("/athlete-event")
    public Result<EventCheckinVO> getCheckinByAthleteAndEvent(
            @RequestParam Long athleteId, 
            @RequestParam Long eventId) {
        
        EventCheckinVO result = eventCheckinService.getCheckinByAthleteAndEvent(athleteId, eventId);
        return Result.success(result);
    }

    @Operation(summary = "删除检录记录")
    @DeleteMapping("/{checkinId}")
    public Result<Void> deleteCheckinRecord(@PathVariable Long checkinId) {
        boolean success = eventCheckinService.deleteCheckinRecord(checkinId);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }
}
